﻿//=============================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 作 者：江怀玉
// 日 期：2011-10-11
// 描 述: 通用的数据视图[YUI]
// URL Parameter：
//        entity:对应的实体
//        condition:过滤数据条件
//        order:排序字段
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00   2011/10/11   江怀玉           
//=============================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.DBModule;
using ZX.EHR.Library;
using ZX.BLL.Extend;

namespace ZX.Web.UI.Controls
{
    public partial class DataGrid : ListPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // 权限 
            if (this.UserId == null)
            {
                Response.Redirect("/Error.aspx?message=1&functionId=" + FunctionID);
            }
            if (!CheckFunctionRight())
            {
                Response.Redirect("/Error.aspx?message=2&functionId=" + FunctionID);
            }
        }

        public override void SetButtons()
        {
            //base.SetButtons();
            string functionid = GetStringFromQueryString("functionid", "");
            V_FunctionRight function = (new BaseBussnies(new V_FunctionRight())).GetEntityByCondition("FunctionID='" + functionid + "'") as V_FunctionRight;
            string userID = this.UserId.ToString();
            if (function == null)
            {
                BtnItems = "";
                BtnEvents = "";
                InitBtnEvents = "";
            }
            else
            {
                string[] btns = function.Opration.Split(',');
                string[] btnTexts = function.BarItems.Split(',');
                string[] fns = function.FnItems.Split(',');
                string[] barItems = (new BLL.BUser()).GetFunctions(userID, functionid)[0].Split(',').Distinct().ToArray();
                if (barItems == null)
                {
                    BtnItems = "";
                    BtnEvents = "";
                    InitBtnEvents = "";
                    return;
                }
                if (barItems.Count() == 0)
                {
                    BtnItems = "";
                    BtnEvents = "";
                    InitBtnEvents = "";
                    return;
                }
                //生成按钮
                StringBuilder strHTML = new StringBuilder("");
                for (int i = 0; i < btns.Length; i++)
                {
                    if (barItems.Contains(btns[i]) || this.UserName.ToLower() == "admin")
                    {
                        string strFormater = "<button type=\"button\" id=\"{0}\" name=\"{0}\" onclick=\"javascript:{2}();\">{1}</button>\n";
                        strHTML.Append(string.Format(strFormater, btns[i], btnTexts[i],fns[i]));

                    }
                }
                BtnItems = strHTML.ToString();
            }
        }
    }
}